package net.chathu.airgw.util 
{
	import flash.errors.IOError;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLVariables;
	import net.chathu.airgw.Debug;
	import net.chathu.airgw.event.DataSendEvent;
	import net.chathu.airgw.model.HTTPReader;
	import net.chathu.airgw.model.HTTPResponseData;

	/**
	 * ...
	 * @author ze
	 */
	
	[Event(name = "sidOK", type = "net.chathu.airgw.event.DataSendEvent")] 
	[Event(name = "sidFail", type = "net.chathu.airgw.event.DataSendEvent")] 

	public class LoginHelper extends EventDispatcher
	{
		public static const SID_OK:String = "sidOK";
		public static const SID_FAIL:String = "sidFail";
		
		private var _cookie:String = null;
		public function LoginHelper() 
		{

		}
		
		public function getSID(username:String, password:String):void
		{
			Debug.echo("GetSID: " + username);
			//String[][] postarray = new String[][]{{"success_redir",""},{"username",post_username},{"password",post_password}};
			//trace("username: " + encodeURIComponent(username));
			//trace("password: " + encodeURIComponent(password));
			var postdata:URLVariables = new URLVariables("success_redir=&username=" + encodeURIComponent(username) + "&password=" + encodeURIComponent(password));
			//trace("Postdata: " + postdata.decode);
			
			var reader:HTTPReader = new HTTPReader();
			reader.addEventListener(IOErrorEvent.IO_ERROR, onHTTPError);
			reader.addEventListener(Event.COMPLETE, onLoginResponse);
			reader.loadUrl("http://chat.hu/belepes", null, "http://chat.hu/", postdata);
		}
		
		private function onLoginResponse(event:Event):void
		{
			var responseData:HTTPResponseData = HTTPReader(event.target).httpResponseData;
			var data:String = responseData.data;
			//trace("data:" + data);
			if (data.indexOf("success") > 0)
			{
				//Sikeres login
				
				var cookie:String = responseData.cookie;
				
				var reader:HTTPReader = new HTTPReader();
				reader.addEventListener(IOErrorEvent.IO_ERROR, onHTTPError);
				reader.addEventListener(Event.COMPLETE, onSiteReloadResponse);
				reader.loadUrl("http://chat.hu/", cookie, "http://chat.hu/belepes", null);
			} else {
				dispatchEvent(new DataSendEvent(SID_FAIL, "loginFail"));
			}
			
		}
	
			
		private function onSiteReloadResponse(event:Event):void
		{
			var responseData:HTTPResponseData = HTTPReader(event.target).httpResponseData;
			//trace("LOC: " + responseData.location);
			//itt volt meg egy location-os atiranyitas..
			
			
			_cookie = responseData.cookie;
				
			var reader:HTTPReader = new HTTPReader();
			reader.addEventListener(IOErrorEvent.IO_ERROR, onHTTPError);
			reader.addEventListener(Event.COMPLETE, onLoadChainComplete1);
			reader.loadUrl("http://chat.hu/client_start", _cookie, "http://chat.hu", null);
		}
		
		
		private function onLoadChainComplete1(event:Event):void
		{
			/*var responseData:HTTPResponseData = HTTPReader(event.target).httpResponseData;
			var cookie:String = responseData.cookie;*/
			var reader:HTTPReader = new HTTPReader();
			reader.addEventListener(IOErrorEvent.IO_ERROR, onHTTPError);
			reader.addEventListener(Event.COMPLETE, onLoadChainComplete2);
			reader.loadUrl("http://chat.hu/Client/init?site=chat", _cookie, "http://chat.hu/client_start", null);
		}
		
		private function onLoadChainComplete2(event:Event):void
		{
			/*var responseData:HTTPResponseData = HTTPReader(event.target).httpResponseData;
			var cookie:String = responseData.cookie;*/
			var reader:HTTPReader = new HTTPReader();
			reader.addEventListener(IOErrorEvent.IO_ERROR, onHTTPError);
			reader.addEventListener(Event.COMPLETE, onLoadChainComplete3);
			reader.loadUrl("http://chat.hu/Client/frame", _cookie, "http://chat.hu/client_start", null);
		}
		private function onLoadChainComplete3(event:Event):void
		{
			/*var responseData:HTTPResponseData = HTTPReader(event.target).httpResponseData;
			var cookie:String = responseData.cookie;*/
			var reader:HTTPReader = new HTTPReader();
			reader.addEventListener(IOErrorEvent.IO_ERROR, onHTTPError);
			reader.addEventListener(Event.COMPLETE, onLoadChainComplete4);
			reader.loadUrl("http://chat.hu/Client/banner", _cookie, "http://chat.hu/client_start", null);
		}
		private function onLoadChainComplete4(event:Event):void
		{
			/*var responseData:HTTPResponseData = HTTPReader(event.target).httpResponseData;
			var cookie:String = responseData.cookie;*/
			var reader:HTTPReader = new HTTPReader();
			reader.addEventListener(IOErrorEvent.IO_ERROR, onHTTPError);
			reader.addEventListener(Event.COMPLETE, onLoadChainComplete5);
			reader.loadUrl("http://chat.hu/Client/median", _cookie, "http://chat.hu/client_start", null);
		}
		private function onLoadChainComplete5(event:Event):void
		{
			/*var responseData:HTTPResponseData = HTTPReader(event.target).httpResponseData;
			var cookie:String = responseData.cookie;*/
			var reader:HTTPReader = new HTTPReader();
			reader.addEventListener(IOErrorEvent.IO_ERROR, onHTTPError);
			reader.addEventListener(Event.COMPLETE, onSIDDataResponse);
			reader.loadUrl("http://chat.hu/Client/java", _cookie, "http://chat.hu/client_start", null);
		}
		
		private function onSIDDataResponse(event:Event):void
		{
			var responseData:HTTPResponseData = HTTPReader(event.target).httpResponseData;
			//trace("VEGE:" + responseData.data);
			var responseText:String = responseData.data;
			var sid:String = searchString(responseText, "&sid=", 16);
			if ( sid == null)
			{
				sid = searchString(responseText, "<param name=\"sid\" value=\"", 16);
				if ( sid == null )
				{
					dispatchEvent(new DataSendEvent(SID_FAIL, "noSid"));
					return;
				}
			}
			
			if (sid.indexOf(";")>0)
			{
				sid=sid.substring(0,sid.indexOf(";"));
			}
			
			trace("SID: " + sid);
			dispatchEvent(new DataSendEvent(SID_OK, sid));
			
		}
		
	
		private function onHTTPError(event:IOErrorEvent):void
		{
			dispatchEvent(new DataSendEvent(SID_FAIL, "httpError"));
		}
		
		private function searchString(text:String, tag:String, length:int):String {
			var pos:int;
			if ( (pos = text.indexOf(tag)) >= 0) {
				return text.substr(pos + tag.length, length);
			}
			return null;
		}
		
	}

}